<!--  -->
<template>
  <div>
    <el-table
      :data="model.menus"
      border
      max-height="800"
    >
      <el-table-tree-column
        :expand-all="!1"
        fixed
        prop="label"
        label="名称"
        width="320"
        header-align="center"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        prop="typeName"
        label="类型"
        width="100"
        align="center"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        prop="path"
        label="路径"
        width="210"
        align="center"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        prop="componentPath"
        label="vue组件路径"
        width="250"
        align="center"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        prop="permission"
        label="权限字符串"
        width="220"
        align="center"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        fixed="right"
        label="操作"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            v-if="addResourcePermit"
            type="text"
            size="small"
            @click="showAddResourceForm(scope.row)"
          >新增</el-button>
          <el-button
            v-if="editResourcePermit"
            type="text"
            size="small"
            @click="showEditResourceForm(scope.row)"
          >编辑</el-button>
          <el-button
            type="text"
            size="small"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :visible.sync="dialogFormVisible"
      title="新增资源"
    >
      <el-form :model="form">
        <el-row :gutter="20">
          <el-col
            v-show="form.parentResoShow"
            :span="20"
          >
            <el-form-item
              label="父资源名称:"
              label-width="150px"
            >
              {{ form.parentResoName }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20">
            <el-form-item
              label="资源名称:"
              label-width="150px"
            >
              <el-input
                v-model="form.resoName"
                autocomplete="off"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20">
            <el-form-item
              label="资源类型:"
              label-width="150px"
            >
              <el-select
                v-model="form.resoTypeCode"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in resoTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20">
            <el-form-item
              label="资源路径:"
              label-width="150px"
            >
              <el-input
                v-model="form.path"
                autocomplete="off"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20">
            <el-form-item
              label="前端组件路径:"
              label-width="150px"
            >
              <el-input
                v-model="form.componentPath"
                autocomplete="off"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20">
            <el-form-item
              label="权限字符串:"
              label-width="150px"
            >
              <el-input
                v-model="form.permission"
                autocomplete="off"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="createReso()"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from '../../../utils/request'
import store from '../../../store'
export default {
  data() {
    return {
      model: {
        menus: null
      },
      form: {
        parentResoCode: null,
        parentResoName: null,
        resoName: null,
        path: null,
        componentPath: null,
        permission: null,
        resoTypeCode: null,
        resoCode: null,
        parentResoShow: true
      },
      dialogFormVisible: false,
      resoTypeList: [
        {
          value: '00040003',
          label: '菜单'
        },
        {
          value: '00040004',
          label: '按钮'
        }
      ],
      addResourcePermit: false,
      editResourcePermit: false
    }
  },
  created() {
    this.fetchData()
    if (store.getters.buttons.length > 0) {
      store.getters.buttons.forEach((item, index) => {
        if (item === 'resource:create') {
          this.addResourcePermit = true
        }
        if (item === 'resource:update') {
          this.editResourcePermit = true
        }
      })
    }
  },
  methods: {
    showAddResourceForm(data) {
      this.dialogFormVisible = true
      this.form.parentResoCode = data.id
      this.form.parentResoName = data.label
    },
    showEditResourceForm(data) {
      this.form.parentResoShow = false
      this.dialogFormVisible = true
      this.form.resoCode = data.id
      this.form.resoName = data.label
      this.form.resoTypeCode = data.resoTypeCode
      this.form.permission = data.permission
      this.form.componentPath = data.componentPath
      this.form.path = data.path
    },
    fetchData() {
      this.listLoading = true
      request({
        url: '/resource/findMenus',
        method: 'get'
      }).then(response => {
        this.model.menus = response.data
        this.listLoading = false
      })
    },
    createReso() {
      this.listLoading = true
      request({
        url: '/resource/createResource',
        method: 'post',
        data: {
          parentResoCode: this.form.parentResoCode,
          permission: this.form.permission,
          path: this.form.path,
          componentPath: this.form.componentPath,
          resoTypeCode: this.form.resoTypeCode,
          resoName: this.form.resoName
        }
      }).then(response => {
        this.listLoading = false
        this.dialogFormVisible = false
        this.fetchData()
      })
    }
  }
}
</script>
<style lang='scss' scoped>
body p {
  background-color: #f5f5f5;
  border-left: 4px solid #ccc;
  line-height: 40px;
  padding-left: 10px;
}

.icon-file {
  padding-right: 7px;
  padding-left: 18px;
}

.icon-folder,
.icon-folder-open {
  padding-left: 7px;
  padding-right: 7px;
}
</style>
